package com.exam.examtest.activity

import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.exam.examtest.R
import com.exam.examtest.adapter.HttpRecordAdapter
import com.exam.examtest.util.ConstantValue
import com.exam.examtest.util.DBHelper
import kotlinx.android.synthetic.main.activity_http_record.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.mRecyclerView
import kotlin.math.min

/**
 * ClassName HttpRecordActivity
 * Create by rhf
 * Create on 2021/4/5 14:18
 * Description 请求记录数据页面
 */
class HttpRecordActivity : BaseActivity() {

    private var manager: LinearLayoutManager? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_http_record)

        init()
        initData()
    }

    /**
     * 初始化一些操作
     */
    private fun init() {
        manager = LinearLayoutManager(this)
        manager!!.orientation = LinearLayoutManager.VERTICAL
        mRecyclerView.layoutManager = manager


        ivBack.setOnClickListener(View.OnClickListener {
            finishActivity()
        })
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finishActivity()
            return false
        }
        return super.onKeyDown(keyCode, event)
    }

    /**
     * 关闭页面
     */
    private fun finishActivity() {
        val mIntent = Intent()
        setResult(20, mIntent)
        finish()
    }

    /**
     * 获取数据
     */
    private fun initData() {
        val listData =
            DBHelper.getInstance().queryRecordByType(ConstantValue.TYPE_HTTP)
        val httpRecordAdapter = HttpRecordAdapter(R.layout.item_http_record, listData)
        mRecyclerView.adapter = httpRecordAdapter
    }
}